#include <stdio.h>
#include "lcv/gpio.h"
#include "test-gpio.h"


#define OUT_1_PROBLEM


void gpio_write_simulator(unsigned char port, unsigned char value)
{
	printf("GPIO Event-Handler was called:\n");
	printf(" -> gpio_write_simulator(%u, %u)\n", port, value);
}

void test_gpio()
{
	printf("reading port IN_0: %x\n", lcv_gpio_read(IN_0));
	printf("reading port IN_1: %x\n", lcv_gpio_read(IN_1));
	
	printf("writing 1 to port OUT_0\n");
	fflush(stdout);
	if(lcv_gpio_write(OUT_0, 1) < 0) {
		printf("error detected\n");
	}
	
	printf("writing 0 to port OUT_0\n");
	fflush(stdout);
	if(lcv_gpio_write(OUT_0, 0) < 0) {
		printf("error detected\n");
	}
	
	#ifndef OUT_1_PROBLEM
		printf("writing 1 to port OUT_1\n");
		if(lcv_gpio_write(OUT_1, 1) < 0) {
			printf("error detected\n");
		}
		printf("writing 0 to port OUT_1\n");
		if(lcv_gpio_write(OUT_1, 0) < 0) {
			printf("error detected\n");
		}
	#endif
	
	printf("simulating input 0 at port 0\n");
	lcv_gpio_simulate_input(IN_0, 0);
	printf("reading port IN_0: %x\n", lcv_gpio_read(IN_0));
	
	printf("setting output event-handler\n");
	lcv_gpio_simulate_output(&gpio_write_simulator);
	
	printf("writing 1 to port OUT_0\n");
	lcv_gpio_write(OUT_0, 1);
			
	//blink_gpio();
}


void blink_gpio()
{
	printf("Start Blinking\n");
	while(TRUE){
		printf("writing 1 to port OUT_0\n");
		lcv_gpio_write(OUT_0, 1);
		usleep(2000000);
		printf("writing 0 to port OUT_0\n");
		lcv_gpio_write(OUT_0, 0);
		usleep(2000000);
	}
}
